package com.bw.module_message

import android.graphics.Paint
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bw.lib_common.base.BaseFragment
import com.bw.lib_common.base.BaseViewModel
import com.bw.module_message.databinding.FragmentMessageBinding
import com.bw.module_message.im.ClubConversationFragment
import com.bw.module_message.notification.NotificationCenterFragment
import com.flyco.tablayout.listener.OnTabSelectListener

/**
 * 消息页面
 */
@Route(path = Router.Ui.MessageFragment)
class MessageFragment : BaseFragment<BaseViewModel,FragmentMessageBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.fragment_message

    private val mTabTitle: List<String> by lazy {
        listOf("聊天", "通知")
    }

    val fragments by lazy {
        listOf(
            // 聊天
            ClubConversationFragment(),
            // 通知
            NotificationCenterFragment()
        )
    }
    private var mAdapter:MessageFragmentAdapter? = null

    override fun initView() {
        super.initView()
        mAdapter = MessageFragmentAdapter(this)
        mBinding.vp.adapter = mAdapter
        mBinding.tabLayout.setViewPager(mBinding.vp,mTabTitle)
    }

    override fun initListener() {
        super.initListener()
        //跳转到通知设置页面
        mBinding.toolbarRightIcon.setOnClickListener {

        }
        mBinding.tabLayout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(p0: Int) {
                updateTabView(p0)

            }

            override fun onTabReselect(p0: Int) {

            }
        })

        mBinding.vp.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                updateTabView(position)
            }
        })
    }

    inner class MessageFragmentAdapter(fa:Fragment):FragmentStateAdapter(fa){
        override fun getItemCount(): Int {
            return fragments.size
        }

        override fun createFragment(p0: Int): Fragment {
            return fragments[p0]
        }

    }

    //缩放
    private fun updateTabView(position: Int) {
        val tabCount: Int = mBinding.tabLayout.tabCount
        for (i in 0 until tabCount) {
            val title: TextView = mBinding.tabLayout.getTitleView(i)
            if (i == position) {
                title.scaleX = 1.25f
                title.scaleY = 1.25f
                title.paint.style = Paint.Style.FILL_AND_STROKE
                title.paint.strokeWidth = 0.3f

            } else {
                title.scaleX = 1.0f
                title.scaleY = 1.0f
                title.paint.style = Paint.Style.FILL_AND_STROKE
                title.paint.strokeWidth = 0f
            }
        }
    }

}